package com.ruoyi.web.controller.tiebauser.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.web.controller.tiebauser.domain.TiebaUsers;
import com.ruoyi.web.controller.tiebauser.mapper.TiebaUsersMapper;
import com.ruoyi.web.controller.tiebauser.service.ITiebaUsersService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.common.utils.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 贴吧用户Service业务层处理
 *
 * @author yong
 * @date 2025-03-09
 */
@Service
public class TiebaUsersServiceImpl extends ServiceImpl<TiebaUsersMapper, TiebaUsers> implements ITiebaUsersService {

    @Autowired
    private TiebaUsersMapper tiebaUsersMapper;

    /**
     * 查询贴吧用户列表
     *
     * @param tiebaUsers 贴吧用户
     * @return 贴吧用户
     */
    @Override
    public List<TiebaUsers> selectTiebaUsersList(TiebaUsers tiebaUsers) {
        List<TiebaUsers> tiebaUsersList = tiebaUsersMapper.selectList(buildQueryWrapper(tiebaUsers));
        return tiebaUsersList;
    }


    /**
     * 用户登录
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public TiebaUsers tiebaLogin(String username, String password) {
        LambdaQueryWrapper<TiebaUsers> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TiebaUsers::getUsername, username);
        lambdaQueryWrapper.eq(TiebaUsers::getPassword, password);
        TiebaUsers tiebaUsers = tiebaUsersMapper.selectOne(lambdaQueryWrapper);
        return tiebaUsers;
    }

    @Override
    public TiebaUsers getByUserName(String username) {
        LambdaQueryWrapper<TiebaUsers> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TiebaUsers::getUsername, username);
        TiebaUsers tiebaUsers = tiebaUsersMapper.selectOne(lambdaQueryWrapper);
        return tiebaUsers;
    }


//    private LambdaQueryWrapper<TiebaUsers> buildQueryWrapper(TiebaUsers query) {
//        Map<String, Object> params = query.getParams();
//        LambdaQueryWrapper<TiebaUsers> lqw = Wrappers.lambdaQuery();
//        lqw.like(StringUtils.isNotBlank(query.getUsername()), TiebaUsers::getUsername, query.getUsername());
//        lqw.eq(StringUtils.isNotBlank(query.getEmail()), TiebaUsers::getEmail, query.getEmail());
////        lqw.eq(TiebaUsers::getDeptId, query.getDeptId());
//        lqw.orderByDesc(TiebaUsers::getCreateTime);
//        return lqw;
//    }

    private LambdaQueryWrapper<TiebaUsers> buildQueryWrapper(TiebaUsers query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<TiebaUsers> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(query.getUsername()), TiebaUsers::getUsername, query.getUsername());
        lqw.orderByDesc(TiebaUsers::getCreateTime);
        return lqw;
    }


}
